<?php
include '../includes/cPlayer.php';
include '../includes/cDatabase.php';
include '../includes/variables.php';
include '../includes/cPage.php';
include '../includes/cDates.php';

$cPage = new page("Get Inactive Players");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

if (@$_GET['form'] == "yes")
{
	// Check user and password
	if ($_POST['fileName'] && $_POST['selectPosition'])
	{
		echo "<BR>fileName:".$_POST['fileName'];
		echo "<BR>selectPosition:".$_POST['selectPosition'];
		searchFileForString($_POST['fileName'], $_POST['selectPosition']);
	}
	else
	{
		echo "ERROR: Parameters Missing";
	}
	?>
	<div align="center">
	<form action="<?php echo $_SERVER['PHP_SELF'] ?>?form=yes" method="POST">
	  <table width='400' border='0'>
		<tr>
		  <td colspan="2" align="center">
			  <INPUT TYPE="BUTTON" VALUE="Main Menu" ONCLICK="window.location.href='http://localhost'"></td>
	 	</tr>
	  </table>
	</form>
	</div>
	
	<?php
}
else
{
$cDatabase = new database();

$connect = $cDatabase->connectToDatabase();
$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
$db = $cDatabase->selectDatabase($connect, $activeDatabase);

$selectArray[0] = '*';
$result = $cDatabase->selectFromDatabase($connect, 'playerlist', $selectArray, NULL, NULL, NULL, NULL);
	

?>
<div align="center">
<form action="<?php echo $_SERVER['PHP_SELF'] ?>?form=yes" method="POST">
  <table width='400' border='0'>
    <tr>
      <td><div align="center">Filename</div></td>
    </tr>
    <tr>
      <td><div align="center">
	    <select name="selectPosition">
			<option value = 'GK'>GK</option>
			<option value = 'DEF'>DEF</option>
			<option value = 'MID'>MID</option>
			<option value = 'ATT'>ATT</option>
		</select>
        <input type='text' name='fileName' size='20' maxlength='30' value=<?php echo @$_POST['fileName'] ?>>
      </div></td>
    </tr>
	<tr>
	  <td colspan="2" align="center">
	    <p>&nbsp;</p>
	    <p><input name="loginButton" type="submit" id="login" value="UPDATE">
        </p></td>
 	</tr>
  </table>
</form>
</div>

<?php
}


function searchFileForString($file,$position) 
{
	$playerCode = array();
	$playerCount = 0;
	$section = file_get_contents($file, true);
	if ($section != FALSE)
	{	
		// e,g,
		// <td><a href='/statistics/points/1001'>van der Sar, E</a></td>
		//<td><a href='/statistics/club-by-club/MUN'>Manchester United</a></td>
		//<td>&pound;4m</td>
		//<td>0.0</td>
		//<td>0</td>
		
		$PlayerIdPattern = "/a href='\/statistics\/points\//";
		$PlayerIdFixedOffset = strlen($PlayerIdPattern) - 5;
		
		$offset = 0;
		
		while(preg_match($PlayerIdPattern, $section, $matches, PREG_OFFSET_CAPTURE, $offset))
		{
			$PlayerIdOffset = $matches[0][1] + $PlayerIdFixedOffset;		// Index into $section string to where the player code ID starts
	
			$PlayerCodeArray = array($section[$PlayerIdOffset],$section[$PlayerIdOffset+1],$section[$PlayerIdOffset+2],$section[$PlayerIdOffset+3]);
			
			$playerCode[$playerCount] = implode($PlayerCodeArray);
			//echo "<BR>code: ".$playerCode[$playerCount];
			$playerCount++;
			
			$offset = $PlayerIdOffset;
			
			// Count four lots of <td>
			for ($count = 0; $count <= 3; $count++)
			{
				if (preg_match("/<td>/", $section, $matches, PREG_OFFSET_CAPTURE, $offset))
				{
					$offset = $matches[0][1]+4;
				}
			}
		}
		
		// go through player list and see if there are any players that have been removed.
		$cDatabase = new database();

		$connect = $cDatabase->connectToDatabase();
		$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
		$db = $cDatabase->selectDatabase($connect, $activeDatabase);
		
		$selectArray[0] = '*';
		$result = $cDatabase->selectFromDatabase($connect, 'playerlist', $selectArray, "playerPosition", $position, NULL, NULL);
		
		for ($i = 0; $i < mysqli_num_rows($result); $i++)
		{
			//echo"<BR>db code ".$row['playerCode'];
			$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
			$found = 0;
			
			for ($count = 0; $count < $playerCount; $count++)
			{
				//echo"<BR>db".$row['playerCode']." tele ".$tempPlayerCode[$count];
				if ($playerCode[$count] == $row['playerCode'])
				{
					// The player from the website is in the database so break out
					$found = 1;
					break;
				}
			}
			if ($found == 0)
			{
				// This player has been removed from the Telegraph website but it's in our database so set it to inactive.
				//echo"<BR>Player need to go:".$row['playerCode'];
				$cDatabase->updatePlayer ($connect, 'playerActive', 0, $row['playerCode']);
			}
		}
	}
	else
	{
		echo "<BR>ERROR: opening file ".$file;
	}	
}

echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>
